<?php
	// SET VALUE DEFAULT FOR user_id
	$user_id = 1;
	
	$form = $_POST;
	$partner = new Partner();
	$partner->setname($form['partner_name']);
	$partner->setdescription($form['description']);
	$partner->setcontent($form['content']);
	$partner->setactive(isset($form['active']) ? 1 : 0);
	if (!$partner_id)
		$partner->setcreated_at(date("Y/m/d"));
	$partner->setupdated_at(date("Y/m/d"));
	$partner->setuser_id($user_id);
	
	$prefixes = array();
	$prefixes[] = 'tour_price_';
	
	$pic_id = check_and_upload_picture('thumbnail', $form['thumbnail'], $HTTP_POST_FILES, $prefixes);
	if ($pic_id) {
		// TODO: remove the old one
		if ($partner_id) {
			$partner->select($partner_id);
			$old_pic_id = $partner->getthm_picture();
			$partner->setthm_picture($pic_id);
			$partner->update();
			if ($old_pic_id) {
				$old_pic = new Picture();
				$old_pic->select($old_pic_id);
				$old_pic->deleteAllFiles();
				$old_pic->delete();
			}
		}
		$partner->setthm_picture($pic_id);
		
	} elseif ($partner_id) {
		$tmp_partner = new Partner();
		$tmp_partner->select($partner_id);
		$partner->setthm_picture($tmp_partner->getthm_picture());
	}
		
	if ($partner_id) { // it's an update action
		$partner->update($partner_id);
	} else { // it's an create action
		$partner->insert();
	}

	$msg = $partner_id ? "Đối tác đã được sửa thành công" : "Đối tác đã được thêm thành công";
	admin_load($msg, "?act=partners_manager");
?>